package com.sophialex.health.record.common.showKtable;

import com.sophialex.health.common.platform.util.TimeHelper;

import de.kupzog.ktable.KTableSortComparator;
import de.kupzog.ktable.KTableSortedModel;

public class ShowKTableSortComparator extends KTableSortComparator {

	public ShowKTableSortComparator(KTableSortedModel model, int columnIndex, int direction) {
        super(model, columnIndex, direction);
    }

    /* (non-Javadoc)
     * @see de.kupzog.ktable.KTableSortComparator#doCompare(java.lang.Object, java.lang.Object, int, int)
     */
    public int doCompare(Object o1, Object o2, int row1, int row2) {     
    	
    	String s1,s2;
    	
    	if(o1==null)
    		s1="0";
    	else
    		s1=o1.toString();
    	
    	if(o2==null)
    		s2="0";
    	else
    		s2=o2.toString();
    	
    	Class fieldType=o1.getClass();
    	
    	if (fieldType.equals(Byte.class)) {
    		byte v1=Byte.parseByte(s1);
    		byte v2=Byte.parseByte(s2);
    		return (v1-v2>0)?1:(v1-v2<0)?-1:0;
		} else if (fieldType.equals(Short.class) ) {
			short v1=Short.parseShort(s1);
			short v2=Short.parseShort(s2);
			return (v1-v2>0)?1:(v1-v2<0)?-1:0;
		} else if (fieldType.equals(Integer.class)) {
			int v1=Integer.parseInt(s1);
			int v2=Integer.parseInt(s2);
			return (v1-v2>0)?1:(v1-v2<0)?-1:0;
		} else if (fieldType.equals(Long.class)) {
			long v1=Long.parseLong(s1);
			long v2=Long.parseLong(s2);
			return (v1-v2>0)?1:(v1-v2<0)?-1:0;
		} else if (fieldType.equals(Float.class)) {
			float v1=Float.parseFloat(s1);
			float v2=Float.parseFloat(s2);
			return (v1-v2>0)?1:(v1-v2<0)?-1:0;
		} else if (fieldType.equals(Double.class)) {
			double v1=Double.parseDouble(s1);
			double v2=Double.parseDouble(s2);			
			return (v1-v2>0)?1:(v1-v2<0)?-1:0;			
		} else if(fieldType.equals(java.util.Date.class)) { 
			s1=TimeHelper.dateToString((java.util.Date) o1);
			s2=TimeHelper.dateToString((java.util.Date) o2);
			return s1.compareTo(s2);
    	}else{
			return s1.compareTo(s2);
		}
    	
    }

}
